package com.mooc.user.controller;

import com.mooc.user.common.RestResponse;
import com.mooc.user.exception.IllegalParamsException;
import com.mooc.user.model.User;
import com.mooc.user.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("user")
public class UserController {

    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Value("${server.port}")
    private Integer port;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private UserService userService;

    @RequestMapping("getusername")
    public RestResponse<String> getusername(Long id){
          logger.info("Request comming, and my server port is" + port);
          redisTemplate.opsForValue().set("key1","val1");
          logger.info("Test Redis :" + redisTemplate.opsForValue().get("key1"));
          if(id == null){
              throw new IllegalParamsException(IllegalParamsException.Type.WORNG_PAGE_NUM,"错误分页");
          }
          return RestResponse.success("test-username" + port);
    }


    @RequestMapping("getUserById")
    public RestResponse<User> getUserById(Long id){
        User user = userService.getUserById(id);
        return RestResponse.success(user);
    }

    @RequestMapping("getList")
    public RestResponse<List<User>> getUserList(@RequestBody User user){
        List<User> users = userService.getUserByQuery(user);
        return RestResponse.success(users);
    }

    //-------------------------------1.注册-----------------
    @RequestMapping("add")
    public RestResponse<User> add(@RequestBody User user){
         userService.addAccount(user,user.getEnableUrl());
         return RestResponse.success();
    }

    //------------------------2.激活-----------------------
    @RequestMapping("enable")
    public RestResponse<User> enable(String key){
        userService.enable(key);
        return RestResponse.success();
    }

    //------------------------3.登录/鉴权--------------------
    @RequestMapping("auth")
    public RestResponse<User> auth(@RequestBody User user){
        User finalUser = userService.auth(user.getEmail(),user.getPasswd());
        return RestResponse.success(finalUser);
    }


    @RequestMapping("get")
    public RestResponse<User> getUser(String token){
        User finalUser  = null;
        try {
            finalUser = userService.getLoginedUserByToken(token);
        } catch (Exception e) {
            logger.info(e.getMessage(),e);
        }
        return RestResponse.success(finalUser);
    }

    //登出
    @RequestMapping("logout")
    public RestResponse<Object> logout(String token){
        userService.invalidate(token);
        return RestResponse.success();
    }

    //test
    @RequestMapping("test")
    public RestResponse<String> test(){
        return RestResponse.success();
    }


}
